﻿namespace OneWorks.Utils.AdoNet
{
    using System.Collections.Generic;

    /// <summary>   
    /// DbProviderFactory工厂类   
    /// </summary>   
    public class ProviderFactory
    {
        private static readonly Dictionary<DbProviderType, string> ProviderInvariantNames = new Dictionary<DbProviderType, string>();
        private static readonly Dictionary<DbProviderType, System.Data.Common.DbProviderFactory> ProviderFactoies = new Dictionary<DbProviderType, System.Data.Common.DbProviderFactory>(20);

        static ProviderFactory()
        {
            //加载已知的数据库访问类的程序集   
            ProviderInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
            ProviderInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
            ProviderInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
            ProviderInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
            ProviderInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
            ProviderInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
            ProviderInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
            ProviderInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
            ProviderInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
            ProviderInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
            ProviderInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
        }

        /// <summary>   
        /// 获取指定数据库类型对应的程序集名称   
        /// </summary>   
        /// <param name="providerType">数据库类型枚举</param>   
        /// <returns></returns>   
        public static string GetProviderInvariantName(DbProviderType providerType)
        {
            return ProviderInvariantNames[providerType];
        }

        /// <summary>   
        /// 获取指定类型的数据库对应的DbProviderFactory   
        /// </summary>   
        /// <param name="providerType">数据库类型枚举</param>   
        /// <returns></returns>   
        public static System.Data.Common.DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
        {
            lock (ProviderFactoies)
            {
                if (!ProviderFactoies.ContainsKey(providerType))
                {
                    ProviderFactoies.Add(providerType, ImportDbProviderFactory(providerType));
                }
                //如果还没有加载，则加载该DbProviderFactory   
                return ProviderFactoies[providerType];
            }
        }
        /// <summary>   
        /// 加载指定数据库类型的DbProviderFactory   
        /// </summary>   
        /// <param name="providerType">数据库类型枚举</param>   
        /// <returns></returns>   
        private static System.Data.Common.DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
        {
            var providerName = ProviderInvariantNames[providerType];
            System.Data.Common.DbProviderFactory factory;
            try
            {
                
                //从全局程序集中查找   
                factory = System.Data.Common.DbProviderFactories.GetFactory(providerName);
            }
            catch
            {
                factory = null;
            }
            return factory;
        }
    }
}
